package com.xuenci.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.xuenci.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")   // 使用这个可以在配置文件中配置alipay.xxx就可以配置这个类里面的属性了
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000120617983";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCjW90trC74XxZkUBZZfl9tZPErVmXC39kGZMG1ec8prPadZ0GlmCj1kGjygeLWf4rPjKhBp+W4fTR1zRGOMguZFY5vPfhhIUM2WkjebkDXzTSsN7bb2cnaZwmY0sxGb+aGgq3qseS8Q43z5fbl/gifBOEd3ZP5aJx/iSs9QOCW/2G68tlF5aDSxYzo4WvMfv3giW0gPCbQjwBHXTfJq/7i7WVGVZ+EE3LxmT0DkWw+rJwSjuLo/IDOfhOX65joLKFWYLP6eKWAUZiAv+lv9oNGegZMhTYt8FDnQdONk0trAfTVQFi2I0JLPFZeITM5HFGo5Z4G6NNczACPa8ODiUV1AgMBAAECggEBAJVj5C/pX4b5hkO0TcwH2oLzJe3I9ASzqwfCyrcILyBgsaMek5U5QnPXXXIwv94DA1Fsw05PmbAOqBP2tlBAigS0Bu7KUEObJJknFa58S0ZOvhWkWdBSKdtWERu14uOjX8Cw7xVLG+/jI4ThT9niZ8Xss2YZhUI3na6wJV+93eWXJviVqPe8XkDhuVhme34glhqXzr1Ja7qva2N9jHA1POoQ/y0J+hwQmc/DUnUWY22oyxGMqjS5zfO7sbLzrlHXuF3RHDCEqW8p9ILrsg+7s3bWnnLfN7dMDVeAjF1puJ5Wx0/K5dqY8xgdE4Fz31xfiH1PPC9C42ayHSzbArB1DiECgYEA+lDwehcFKkGJ380eQtkIFWJ0YXDPTsTYktWJ6ytd5vgK8Jh9M9qPR13Bv52ELOVRCbjR1doFUCEdu5ppEIjG4MC1NXi9baOzv6TaAh8yqJOj6lSpjpa6gm1AIJ3SyLoJaPphi4UXw0KkiGzKLYoIXqBCXi6pNj7mZ4dFaS5XjNkCgYEApxFzfoIVPJ60j6gHTGJfOwmJVG6C74v3U9D/XjWNwR/5gw+UWJX0UCOy6rc8Pm40pJnWOCfUXuri9C92dMkvBpdYtrUn4SXpq7b2SfQ1VBPjJypyMdRIv2VjjcrTNwok2vwzDp+kS50eX5ixdTXGG3ary1+TaYc0+XSTPgULy/0CgYAHxsPxxw1OBsM1xLWP9hYeM/TRoiOOmubviNrEB5DfQA129AXPupaA7RS70zTsACPIXYmHT0Ki/qRh06Y38Ufca6A1jQ61fUR0S7bKIhX8zrlk6kRqPZyFuMcpPD30JnNsY3rsTrIySru50UdmqscfyXLGYkS3frTBup4Zamsl4QKBgHOLGTI8Kw3SwRO9ZNbKDgyZitXO3FVpmrLoD0ULiq8vOvQOeU1O4DnwhpPYCB0xQM0567LIsRK4JiA1Fxqr7Q8E6wDphtwqyVHN5sW81IkCfA4hR/r4UxuztkdhJ/Sf2l9tHq6VcB4vWstIoipq90kVKLktr5jDPrrric3R1ZfNAoGBAMzukmvIpfTiyjLDOvSDdU26z55tnez1TIB3XcPgWTswGxQ9OtPe6F3uz1asMC8HAbRs3RODTovr6qqnRLZf1JEBGD4PJROClK60JCOwB3U/9/4qko3poHbc+/yoYf1qVTKR3kjBw4I1jHa/k6HSA3KilhU959M4TRtkirnNvmJh";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlyRskPCfUrByMw3anpJ5VfdIXnm3PaL7LSf1bAmwStFQ3D6jCeZ9cDq2JikWuPUxyyhZJ8LENPn0hoywsdxwwngd0v6U2/3c17QvBQQsAnfL1A21hFXpVhoklLsy08IjH0K9gw/a+WtesJPk6tntd3TjUh9zBbKiXBHczPIoBB8XtYbKsI7+MJPnWZ2kHHi57lsQU9j3vWW2T5YokhOB1z9ueLKC8L4shvHGUelNIUahx1yi985os/5eJUD2KYssP/5uZsF2nEmNtr8bDjKQlRVyI0ddFA55tuZ/qFON9Xe3pya7qLPvZF6XEvj5NipqDhAnR+OIuUhYJcOABmARtQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="https://4978122mw0.zicp.fun/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    private String timeout = "30m";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
